package fr.charlott.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import fr.charlott.business.dto.PersonneDTO;
import fr.charlott.business.service.PersonneService;
import fr.charlott.tools.Constants;

@Controller("dashboardControler")
@Scope("view")
public class DashboardControler extends CharlottControler {

    private static final long serialVersionUID = 1L;

    private PersonneService personneService;
    /**
     * Liste des anniversaires
     */
    private List<PersonneDTO> anniversaires = new ArrayList<PersonneDTO>();

    private ScheduleModel scheduleModel;

    /**
     * Liste des visites
     */
    private List<PersonneDTO> visites = new ArrayList<PersonneDTO>();

    private PersonneDTO selectedPersonne;

    // @Value("${visits.days.before}")
    // private int visitsDaysBefore;
    //
    // @Value("${visits.days.after}")
    // private int visitsDaysAfter;

    public DashboardControler() {
        super();
    }

    @Autowired(required = true)
    public DashboardControler(PersonneService personneService) {
        this();

        this.personneService = personneService;

        chargerLesAnniversaires();
        chargerLesVisites();
    }

    public void chargerLesAnniversaires() {
        anniversaires = personneService.chargerTousLesAnniversaires();
        scheduleModel = new DefaultScheduleModel();
        for (PersonneDTO personne : anniversaires) {
            if (personne.getAnniversaire() != null) {
                scheduleModel.addEvent(new DefaultScheduleEvent(personne.toString(), personne.getAnniversaire(), personne.getAnniversaire(), true));
            }
        }
    }

    public void chargerLesVisites() {
        Calendar dateFrom = new GregorianCalendar();
        dateFrom.add(Calendar.DATE, -Constants.visitsDaysBefore);
        Calendar dateTo = new GregorianCalendar();
        dateTo.add(Calendar.DATE, -Constants.visitsDaysAfter);

        visites = personneService.chargerProchainesVisites(dateFrom.getTime(), dateTo.getTime());
    }

    public ScheduleModel getScheduleModel() {
        return scheduleModel;
    }

    public List<PersonneDTO> getAnniversaires() {
        return anniversaires;
    }

    public List<PersonneDTO> getVisites() {
        return visites;
    }

    public PersonneDTO getSelectedPersonne() {
        return selectedPersonne;
    }

    public void setSelectedPersonne(PersonneDTO selectedPersonne) {
        this.selectedPersonne = selectedPersonne;
    }

}
